/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bombercloud.node;

import bombercloud.BomberCloudNodeConfig;
import bombercloud.commons.GameAction;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public class BomberCloudNodeThread extends Thread {

    private Socket socket;

    public BomberCloudNodeThread(Socket socket) {
        this.socket = socket;
        try {
            Class.forName(BomberCloudNodeConfig.DATABASE_DRIVER);
        } catch (ClassNotFoundException ex) {
        }
    }

    @Override
    public void run() {
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
            GameAction gameAction = (GameAction) inputStream.readObject();
            try {
                Connection connection = DriverManager.getConnection(BomberCloudNodeConfig.DATABASE_URL, BomberCloudNodeConfig.DATABASE_USERNAME, BomberCloudNodeConfig.DATABASE_PASSWORD);
                Statement statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery("SELECT id FROM player WHERE id = " + gameAction.getPlayerId());
                boolean hasPlayer = false;
                while (resultSet.next()) {
                    hasPlayer = true;
                }
                resultSet.close();
                if (!hasPlayer) {
                    statement.executeUpdate("INSERT INTO player (id, x, y) VALUES (" + gameAction.getPlayerId() + ", " + 0 + ", " + 0 + ")");
                }
                statement.close();
                connection.close();
            } catch (SQLException ex) {
            }
            switch (gameAction.getAction()) {
                case GameAction.UP: {
                    up(gameAction);
                    break;
                }
                case GameAction.DOWN: {
                    down(gameAction);
                    break;
                }
                case GameAction.LEFT: {
                    left(gameAction);
                    break;
                }
                case GameAction.RIGHT: {
                    right(gameAction);
                    break;
                }
            }
            gameAction.setProcessed(true);
            BomberCloudNodeOutputMessage.printMessage("Player " + gameAction.getPlayerId() + "\t Action: " + gameAction.getActionString() + "\t Processed: " + (gameAction.isProcessed()));
            outputStream.writeObject(gameAction);
            outputStream.flush();
            socket.close();
        } catch (IOException ex) {
        } catch (ClassNotFoundException ex) {
        }
    }

    public void up(GameAction gameAction) {
        try {
            Connection connection = DriverManager.getConnection(BomberCloudNodeConfig.DATABASE_URL, BomberCloudNodeConfig.DATABASE_USERNAME, BomberCloudNodeConfig.DATABASE_PASSWORD);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT y FROM player WHERE id = " + gameAction.getPlayerId());
            resultSet.next();
            int y = resultSet.getInt("y");
            resultSet.close();
            if (y == 0) {
                statement.close();
                connection.close();
                return;
            }
            y = y - gameAction.getAvatarHeight();
            statement.executeUpdate("UPDATE player SET y = " + y + " WHERE id = " + gameAction.getPlayerId());
            statement.close();
            connection.close();
        } catch (SQLException ex) {
        }
    }

    public void down(GameAction gameAction) {
        try {
            Connection connection = DriverManager.getConnection(BomberCloudNodeConfig.DATABASE_URL, BomberCloudNodeConfig.DATABASE_USERNAME, BomberCloudNodeConfig.DATABASE_PASSWORD);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT y FROM player WHERE id = " + gameAction.getPlayerId());
            resultSet.next();
            int y = resultSet.getInt("y");
            resultSet.close();
            if (y == gameAction.getDimensionHeight() - gameAction.getAvatarHeight()) {
                statement.close();
                connection.close();
                return;
            }
            y = y + gameAction.getAvatarHeight();
            statement.executeUpdate("UPDATE player SET y = " + y + " WHERE id = " + gameAction.getPlayerId());
            statement.close();
            connection.close();
        } catch (SQLException ex) {

        }
    }

    public void left(GameAction gameAction) {
        try {
            Connection connection = DriverManager.getConnection(BomberCloudNodeConfig.DATABASE_URL, BomberCloudNodeConfig.DATABASE_USERNAME, BomberCloudNodeConfig.DATABASE_PASSWORD);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT x FROM player WHERE id = " + gameAction.getPlayerId());
            resultSet.next();
            int x = resultSet.getInt("x");
            resultSet.close();
            if (x == 0) {
                statement.close();
                connection.close();
                return;
            }
            x = x - gameAction.getAvatarWidth();
            statement.executeUpdate("UPDATE player SET x = " + x + " WHERE id = " + gameAction.getPlayerId());
            statement.close();
            connection.close();
        } catch (SQLException ex) {
        }
    }

    public void right(GameAction gameAction) {
        try {
            Connection connection = DriverManager.getConnection(BomberCloudNodeConfig.DATABASE_URL, BomberCloudNodeConfig.DATABASE_USERNAME, BomberCloudNodeConfig.DATABASE_PASSWORD);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT x FROM player WHERE id = " + gameAction.getPlayerId());
            resultSet.next();
            int x = resultSet.getInt("x");
            resultSet.close();
            if (x == gameAction.getDimensionWidth() - gameAction.getAvatarWidth()) {
                statement.close();
                connection.close();
                return;
            }
            x = x + gameAction.getAvatarWidth();
            statement.executeUpdate("UPDATE player SET x = " + x + " WHERE id = " + gameAction.getPlayerId());
            statement.close();
            connection.close();
        } catch (SQLException ex) {
        }
    }
}
